package com.baiytfp.hf.HFDataExchange.action.dao;

import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.Operate_record;
import com.baiytfp.hf.HFDataExchange.common.utils.HFPagerListSqlUtil;
import com.baiytfp.hf.baiytfp.common.db.HFPager;
import com.baiytfp.hf.baiytfp.common.util.DateUtil;
import com.baiytfp.hf.baiytfp.framework.base.BaseDao;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 操作记录
 */
@Repository
public class OperateRecordDAOImpl extends BaseDao {

    public List<Map> getList(HFPager hfPager) throws Exception {
		List<String> params = new ArrayList<String>();
		String select = " select r.*, u.user_name record_man_name ";
		String from = " from " + TableName.OPERATE_RECORD + " r " +
				" left join " + TableName.USER_INFO + " u on r.record_man = u.user_code ";
		StringBuilder where = new StringBuilder(" where 1 = 1 ");
		HFPagerListSqlUtil hfPagerListSqlUtil = new HFPagerListSqlUtil(hfPager, params, where);

		hfPagerListSqlUtil.whereAppend("record_id", "and r.record_id = ?");
		hfPagerListSqlUtil.whereAppend("record_man", "and r.record_man = ?");
		hfPagerListSqlUtil.whereAppend("record_table_id", "and r.record_table_id = ?");
		hfPagerListSqlUtil.whereAppend("record_type", "and r.record_type = ?");
		hfPagerListSqlUtil.whereAppendLike("record_table_name", "and r.record_table_name like ?");

		hfPagerListSqlUtil.whereAppend("record_time_start", "and r.record_time >= ?");
		hfPagerListSqlUtil.whereAppend("record_time_end", "and r.record_time < ?");


		String sql = select + from + where.toString();
		List<Map> list = HFDBHelper.list_split(hfPagerListSqlUtil.packSql(sql), Map.class, hfPager, params.toArray());
		return list;
    }

	public long add(Operate_record record) throws Exception {
		record.setRecord_id(HFDBHelper.getIDGenerator());
		return HFDBHelper.add(TableName.OPERATE_RECORD, record);
	}
	public long update(Operate_record record) throws Exception {
    	return HFDBHelper.update(TableName.OPERATE_RECORD, "where record_id = ?", record, record.getRecord_id());
	}

	public Operate_record get(String id) throws Exception {
		return HFDBHelper.read(TableName.OPERATE_RECORD, "where record_id = ?", Operate_record.class, id);
	}

}
